package com.tjmc.nsia.core.controller.front;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.tjmc.nsia.core.constant.BusinessType;
import com.tjmc.nsia.core.dao.entity.NsiaChat;
import com.tjmc.nsia.core.domain.AjaxResult;
import com.tjmc.nsia.core.domain.Log;
import com.tjmc.nsia.core.dto.req.ChatBody;
import com.tjmc.nsia.core.service.NsiaChatService;
import com.tjmc.nsia.core.service.NsiaChatSessionService;
import com.tjmc.nsia.core.service.NsiaInteractionService;
import com.tjmc.nsia.core.utils.SseEmitterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.time.LocalDateTime;

/**
 * @author Luc
 * @version 1.0
 * @date 2024-06-21 11:16
 */
@RestController
@RequestMapping("/interaction")
public class NsiaInteractionController {

    @Autowired
    private NsiaInteractionService nsiaInteractionService;

    @Autowired
    private NsiaChatService nsiaChatService;

    @Autowired
    private NsiaChatSessionService nsiaChatSessionService;

//    @Resource
//    private WebSocketChatServer webSocketServer;

    @SaIgnore
    @GetMapping("/connect/{userId}")
    public SseEmitter connect(@PathVariable String userId) {
        return SseEmitterUtils.connect(userId);
    }

    @SaIgnore
    @GetMapping("/closed/{userId}")
    public void closed(@PathVariable String userId) {
        SseEmitterUtils.removeUser(userId);
    }

    @Log(title = "人机交互", businessType = BusinessType.INSERT)
    @PostMapping("/invokeResponse")
    public AjaxResult invokeResponse(@RequestBody NsiaChat nsiaChat) throws JsonProcessingException {
        if(nsiaChat.getStream() == 1) { //同步调用
            nsiaChat.setStartTime(LocalDateTime.now());
            String answer = nsiaInteractionService.syncResponse(nsiaChat.getQuestion());
            if(StringUtils.hasText(answer)) {
                nsiaChat.setAnswer(answer);
                nsiaChat.setEndTime(LocalDateTime.now());
                nsiaChat.setUserId(StpUtil.getLoginIdAsLong());
                nsiaChatSessionService.updateChatSession(nsiaChat);
                nsiaChatService.save(nsiaChat);
                return AjaxResult.success(nsiaChat);
            }
        } else if(nsiaChat.getStream() == 2) { //流式调用
            nsiaChat.setStartTime(LocalDateTime.now());
            String chatId = StpUtil.getTokenValue() + "|||" + System.currentTimeMillis();
            nsiaChat.setChatId(chatId);
            nsiaChat.setUserId(StpUtil.getLoginIdAsLong());
//            nsiaInteractionService.streamResponse(nsiaChat, StpUtil.getTokenValue());
            nsiaChatSessionService.updateChatSession(nsiaChat);
            return AjaxResult.success(nsiaChat);
        }
        return AjaxResult.error();
    }

    @PostMapping(value = {"/chats"})
    public AjaxResult listChats(@RequestBody ChatBody chatBody) {
        return AjaxResult.success(nsiaChatService.listChats(chatBody));
    }
}
